////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Iflytek-ZY-GCZX team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.cums.util;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.iflytek.ci.common.utils.excel.FormatContentParser;
import com.iflytek.ci.cums.dto.ProjectAccount;
import com.iflytek.ci.cums.service.impl.EncryptionPassword;
import com.iflytek.ci.main.dto.Macro;

import java.util.List;

/**
 * <p>
 * Excel导入时，Excel解析的实现类.
 * </p>
 *
 * @author poplar.phwang2
 * @version 1.0 2012-11-18 1:57 PM
 * @since JDK 1.5
 */
public class ProjectAddExcelParser implements FormatContentParser {
    private static String s_role = "默认非导入角色";

    private static int y_column;
    /**
     * 所有的Excel人员信息
     */
    private List<ProjectAccount> account_list = Lists.newArrayList();
    /**
     * 每行的人员信息
     */
    private ProjectAccount line_project_account;

    @Override
    public <T> Object getResult() {
        return account_list;
    }

    @Override
    public void newLine(int lineNumber) {
        line_project_account = new ProjectAccount();
        line_project_account.setPassword(EncryptionPassword.encryption(Macro.DEFAULT_PASSWORD));
        account_list.add(line_project_account);
    }

    @Override
    public void parseUnit(int x, int y, String content) {
        //是否为空
        boolean content_empty = Strings.isNullOrEmpty(content);
        if (!content_empty) {
            content = content.trim();
        }
        switch (x) {
            case 0:
                //如果第一行不为空，则设置角色全局变量为content
                if (!Strings.isNullOrEmpty(content)) {
                    s_role = content;
                    y_column = y;
                    line_project_account.setRole(s_role);
                }
                if (y != y_column) {
                    line_project_account.setRole(s_role);
                }
                break;
            case 2:
                if (y != y_column) {
                       line_project_account.setName(content);
                }
                else{
                    line_project_account.setName(content);
                }
                break;
            case 7:
                try{
                        if (y != y_column ) {
                            if(content.substring(0,content.indexOf("@")).trim().length()!=0)  {
                            line_project_account.setEmail(content);
                            line_project_account.setLogin(content.substring(0,content.indexOf("@")));
                            }
                        }
                    else {
                            line_project_account.setEmail(content);
                            line_project_account.setLogin(content.substring(0,content.indexOf("@")));
                        }
                    }
                    catch (Exception e){
                       break;
                }
                break;
            default:
                break;
        }
    }
}
